package com.eqdd.yiqidian.ui.yiqixue;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.InputType;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.yiqidian.LearnMeActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LearnMineInfo;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.yiqixue.add.AddLectureActivity;
import com.eqdd.yiqidian.ui.yiqixue.add.AddVideoActivity;
import com.eqdd.yiqidian.ui.yiqixue.add.AddXueArticleActivity;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindArticleFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindLectureFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindNeedFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindPhotoFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindVideoFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.LearnMineInfoFragment;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-29  下午4:57.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class LearnMeActivity extends BaseActivity {
    List<Fragment> mFragments;


    List<String> mTitles = new ArrayList<>();
    private LearnMeActivityCustom dataBinding;
    private LearnMineInfo items;
    private Bundle bundle;
    private FindPhotoFragment findPhotoFragment;
    private FindNeedFragment findNeedFragment;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_learn_me);
        initTopTitleBar(View.VISIBLE, "个人中心");

    }

    @Override
    public void initData() {
        mTitles.add("个人信息");
        mTitles.add("课程管理");
        mTitles.add("视频管理");
        mTitles.add("文章管理");
        mTitles.add("照片管理");
        mTitles.add("需求推荐");
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<LearnMineInfo>>post(HttpConfig.BASE_URL + HttpConfig.LEARN_MINE_INFO)
                .params("userGuid", AppConstent.user.getGuid())
                .execute(new DialogCallBack<HttpResult<LearnMineInfo>>(LearnMeActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<LearnMineInfo>> response) {
                        HttpResult<LearnMineInfo> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            items = httpResult.getItems();
                            setupViewPager();
                        }
                    }
                });
    }

    private void setupViewPager() {
        setupViewPager(dataBinding.viewpager);
        dataBinding.slidingTabStrip.setViewPager(dataBinding.viewpager);
    }

    private void setupViewPager(ViewPager viewPager) {
        mFragments = new ArrayList<>();
        addMineInfoFragment();
        addLectureFragment();
        addVideoFragment();
        addArticleFragment();
        addPhotoFragment();
        addNeedFragment();
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (position != 0) {
                    initTopRightText("添加", v -> {
                        switch (position) {
                            case 1:
                                startActivity(AddLectureActivity.class);
                                break;
                            case 2:
                                startActivity(AddVideoActivity.class);
                                break;
                            case 3:
                                startActivity(AddXueArticleActivity.class);
                                break;
                            case 4:
                                new MaterialDialog.Builder(LearnMeActivity.this)
                                        .title("请输入相册名字")
                                        .inputType(InputType.TYPE_CLASS_TEXT)//可以输入的类型-电话号码
                                        .input("相册名字", "", (dialog, input) -> Log.i("yqy", "输入的是：" + input))
                                        .onPositive((dialog, which) -> {
                                            if (dialog.getInputEditText().length() > 0) {
                                                HttpUtil.createLearnPhotoMenu(LearnMeActivity.this, dialog.getInputEditText().getText().toString(), () -> {
                                                    findPhotoFragment.refresh();
                                                });
                                            }
                                        }).show();
                                break;
                        }
                    });
                } else {
                    dataBinding.includeTitle.tvRight.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    private void addPhotoFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_PHOTO_MENU_LIST_MINE);
        bundle.putString(Config.LECTURE_GUID, items.getUserGuid());
        findPhotoFragment = new FindPhotoFragment();
        findPhotoFragment.setArguments(bundle);
        mFragments.add(findPhotoFragment);
    }
    private void addNeedFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_NEED_LIST_MINE);
        bundle.putString(Config.FIELD, items.getResearchField());
        findNeedFragment = new FindNeedFragment();
        findNeedFragment.setArguments(bundle);
        mFragments.add(findNeedFragment);
    }

    private void addArticleFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_ARTICLE_LIST_MINE);
        bundle.putString(Config.LECTURE_GUID, items.getUserGuid());
        FindArticleFragment findArticleFragment = new FindArticleFragment();
        findArticleFragment.setArguments(bundle);
        mFragments.add(findArticleFragment);

    }

    private void addVideoFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_VIDEO_LIST_MINE);
        bundle.putString(Config.LECTURE_GUID, items.getUserGuid());
        FindVideoFragment findVideoFragment = new FindVideoFragment();
        findVideoFragment.setArguments(bundle);
        mFragments.add(findVideoFragment);
    }

    private void addLectureFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_LECTURE_LIST_MINE);
        bundle.putString(Config.LECTURE_GUID, items.getUserGuid());
        FindLectureFragment findLectureFragment = new FindLectureFragment();
        findLectureFragment.setArguments(bundle);
        mFragments.add(findLectureFragment);
    }

    private void addMineInfoFragment() {
        bundle = new Bundle();
        bundle.putSerializable(Config.BEAN, items);
        LearnMineInfoFragment learnMineInfoFragment = new LearnMineInfoFragment();
        learnMineInfoFragment.setArguments(bundle);
        mFragments.add(learnMineInfoFragment);
    }
}
